package com.songshuai.sales.tool.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.songshuai.sales.tool.dto.request.service.item.AddServiceItemReqDto;
import com.songshuai.sales.tool.dto.request.service.item.QueryServiceItemsReqDto;
import com.songshuai.sales.tool.dto.request.service.item.UpdateServiceItemReqDto;
import com.songshuai.sales.tool.dto.response.service.item.ServiceItemDetailResDto;
import com.songshuai.sales.tool.dto.response.service.item.ServiceItemsResDto;
import com.songshuai.sales.tool.model.base.YixueBaseServiceItem;
import com.songshuai.sales.tool.service.YixueBaseServiceItemService;
import com.songshuai.webapi.baseapi.constant.BaseConstant;
import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.exception.BusinessException;
import com.songshuai.webapi.baseapi.pojo.BaseParmDto;
import com.songshuai.webapi.baseapi.pojo.QueryPageRes;
import com.songshuai.webapi.baseapi.pojo.QueryRes;
import com.songshuai.webapi.baseapi.utils.PropertiesUtil;
import com.songshuai.webapi.baseapi.utils.RequestParamValidUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 服务标准化内容管理
 * @Author: lhx
 * @Date: 2019/4/11 17:23
 */
@Slf4j
@Api("服务标准化内容管理")
@RestController
@RequestMapping("/api/serviceItem/")
public class ServiceItemController {


    @Autowired
    private YixueBaseServiceItemService baseServiceItemService;


    /**
     * 分页查询服务列表
     * @param request
     * @return
     */
    @ApiOperation(value = "分页查询服务列表")
    @PostMapping("queryServiceItems")
    public QueryRes<List<ServiceItemsResDto>> queryServiceItems(@RequestBody BaseParmDto<QueryServiceItemsReqDto> request){
        try {
            Page<List<ServiceItemsResDto>> page = PageHelper.startPage(request.getPageIndex(), request.getPageSize());
            List<ServiceItemsResDto> serviceItemsResDtos = baseServiceItemService.queryServiceItemPage(request);
            return QueryPageRes.setQueryResData(serviceItemsResDtos, page.getTotal());
        } catch (Exception e) {
            log.error("/api/serviceItem/queryServiceItems", e);
            throw new BusinessException(ResultStatus.ERROR_SEARCH_FAILED);
        }

    }

    @ApiOperation(value = "根据id获取详情")
    @GetMapping("queryDetail")
    public QueryRes<ServiceItemDetailResDto> queryDetail(@RequestParam("id")Integer id){
        try {
            YixueBaseServiceItem yixueBaseServiceItem = baseServiceItemService.selectByPrimaryKey(id);
            if (null != yixueBaseServiceItem){
                ServiceItemDetailResDto resDto = new ServiceItemDetailResDto();
                PropertiesUtil.copyProperties(yixueBaseServiceItem,resDto);
                resDto.setTypeId(yixueBaseServiceItem.getTypeid());
                return QueryRes.setResData(resDto);
            }
            return new QueryRes<>();
        } catch (Exception e) {
            log.error("/api/serviceItem/queryDetail", e);
            throw new BusinessException(ResultStatus.ERROR_SEARCH_FAILED);
        }

    }


    @ApiOperation(value = "新增服务内容")
    @PostMapping("addServiceItems")
    public QueryRes<Boolean> addServiceItems(@RequestBody @Valid AddServiceItemReqDto reqDto, BindingResult result){
        if (result.hasErrors()){
            return new QueryRes(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorCode(), RequestParamValidUtil.getBindingResultMessage(result));
        }
        try {
            return QueryRes.setResData(baseServiceItemService.addServiceItems(reqDto));
        } catch (Exception e) {
            log.error("/api/serviceItem/addServiceItems", e);
            throw new BusinessException(ResultStatus.ERROR_CREATE_FAILED);
        }
    }


    @ApiOperation(value = "修改服务内容")
    @PostMapping("updateServiceItems")
    public QueryRes<Boolean> updateServiceItems(@RequestBody @Valid UpdateServiceItemReqDto reqDto, BindingResult result){
        if (result.hasErrors()){
            return new QueryRes(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorCode(), RequestParamValidUtil.getBindingResultMessage(result));
        }
        try {
            return QueryRes.setResData(baseServiceItemService.updateServiceItems(reqDto));
        } catch (Exception e) {
            log.error("/api/serviceItem/updateServiceItems", e);
            throw new BusinessException(ResultStatus.ERROR_UPDATE_FAILED);
        }
    }

    @ApiOperation(value = "根据id删除服务内容")
    @GetMapping("deleteServiceItems")
    public QueryRes<Boolean> deleteServiceItems(@RequestParam("id")Integer id){
        try {
            return QueryRes.setResData(baseServiceItemService.updateByPrimaryKey(new YixueBaseServiceItem(){{
                setId(id);
                setIsdelete(BaseConstant.IS_DELETE_FLAG_YES);
            }}) > 0);
        } catch (Exception e) {
            log.error("/api/serviceItem/deleteServiceItems", e);
            throw new BusinessException(ResultStatus.ERROR_DELETE_FAILED);
        }
    }



}
